package com.asbd.springboot.controller;

import cn.hutool.core.util.StrUtil;
import com.asbd.springboot.common.Result;
import com.asbd.springboot.entity.User;
import com.asbd.springboot.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 功能：提供接口，返回数据
 * 作者：ASBD
 * 日期：2024/12/6 20:41
 */

@RestController
public class WebController {

    @Resource
    UserService userService;

    @GetMapping("/")
    private Result hello(){
        return Result.success("hello");
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user){
        if (StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())){
            return Result.error("数据不合法！！！");
        }
        user = userService.login(user);
        return Result.success(user);

    }

    @PostMapping("/register")
    public Result register(@RequestBody User user){
        if (StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())){
            return Result.error("数据不合法！！！");
        }
        if (user.getUsername().length() > 10 || user.getPassword().length() > 20){
            return Result.error("数据输入不合法！！！");
        }
        user = userService.register(user);
        return Result.success(user);

    }
}